//
//  LoginViewController.m
//  zhijian
//
//  Created by lcb on 2019/1/19.
//  Copyright © 2019 lcb. All rights reserved.
//

#import "LoginViewController.h"
#import "Base.h"
#import "LTLimitTextField.h"
#import "AppDelegate.h"
#import "ForgetPwdViewController.h"
@interface LoginViewController ()<UITextFieldDelegate>
@property (nonatomic, strong) UIImageView * loginView;
@property (nonatomic, strong) LTLimitTextField *usernameField;
@property (nonatomic, strong) LTLimitTextField *passwordField;
@property (nonatomic, strong)  UIImageView *usernameline;
@property (nonatomic, strong)  UIImageView *passwordline;
@property (nonatomic, strong) UIButton * sendButton;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg"]];
    bg.frame=self.view.bounds;
    [self.view addSubview:bg];
    [self initUI];
}
-(void)initUI
{
    self.loginView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"白框"]];
    self.loginView.frame = CGRectMake(30, 100, SCREEN_WIDTH- 60, 300);
    self.loginView.contentMode =UIViewContentModeScaleToFill;
    self.loginView.userInteractionEnabled = YES;
    [self.view addSubview:self.loginView];
    //
    UIImageView *avaterView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"avater"]];
    avaterView.frame = CGRectMake((SCREEN_WIDTH-140)/2, 40, 100, 100);
    avaterView.contentMode =UIViewContentModeScaleToFill;
    [self.loginView addSubview:avaterView];
    //
    self.usernameField.frame =CGRectMake(30,avaterView.frame.origin.y+avaterView.frame.size.height+20, self.loginView.frame.size.width-60, 40);
    self.usernameline.frame =CGRectMake(36, self.usernameField.frame.size.height-1, self.usernameField.frame.size.width-36, 1);
    [self.loginView addSubview:self.usernameField];
    //
    self.passwordField.frame =CGRectMake(30,self.usernameField.frame.origin.y+self.usernameField.frame.size.height+10,self.loginView.frame.size.width-60, 40);
    self.passwordline.frame =CGRectMake(36, self.passwordField.frame.size.height-1, self.passwordField.frame.size.width-36, 1);
    [self.loginView addSubview:self.passwordField];
    //
    UIButton *rememberBtn=[[UIButton alloc]initWithFrame:CGRectMake(self.loginView.frame.origin.x, self.loginView.frame.origin.y+self.loginView.frame.size.height+20, 100, 20)];
    [rememberBtn setTitle:@"记住密码" forState:(UIControlStateNormal)];
    [rememberBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [rememberBtn setImage:[UIImage imageNamed:@"record_nor"] forState:UIControlStateNormal];
    [rememberBtn setImage:[UIImage imageNamed:@"record_sel"] forState:UIControlStateSelected];
    [rememberBtn addTarget:self action:@selector(BtnRemember:) forControlEvents:UIControlEventTouchUpInside];
    NSString *isrecord =[[NSUserDefaults standardUserDefaults] objectForKey:@"isRecord"];
    if ([isrecord isEqualToString:@"1"]) {
        [rememberBtn setSelected:YES];
    }else{
         [rememberBtn setSelected:NO];
    }
    [self.view addSubview:rememberBtn];
    rememberBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;
 
    //
    UIButton *forgetButton = [UIButton buttonWithType:UIButtonTypeCustom];
    forgetButton.frame=CGRectMake(self.loginView.frame.origin.x+self.loginView.frame.size.width-100,
                              self.loginView.frame.origin.y+self.loginView.frame.size.height+20, 100, 20);
    [forgetButton setTitle:@"忘记密码?" forState:UIControlStateNormal];
    [forgetButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [forgetButton addTarget:self action:@selector(forgetClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view  addSubview:forgetButton];
    //
   self.sendButton.frame=CGRectMake(self.loginView.frame.origin.x,forgetButton.frame.origin.y+forgetButton.frame.size.height+20, self.loginView.frame.size.width, 50);
    [self.view  addSubview:self.sendButton];
    
}
-(void)BtnRemember:(UIButton*)btn{
    btn.selected = !btn.selected;
    if (btn.selected==YES) {
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        [user setValue:@"1" forKey:@"isRecord"];
        [user setValue:self.usernameField.text forKey:@"usernamefiled"];
        [user setValue:self.passwordField.text forKey:@"passwordfiled"];
        [user synchronize];
    }else{
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        [user setValue:@"2" forKey:@"isRecord"];
        [user setValue:@"" forKey:@"usernamefiled"];
        [user setValue:@"" forKey:@"passwordfiled"];
        [user synchronize];
    }
}

-(LTLimitTextField *)usernameField{
    if (!_usernameField) {
        _usernameField = [[LTLimitTextField alloc]initWithFrame:CGRectZero];
        _usernameField.backgroundColor = [UIColor clearColor];
        _usernameField.font = [UIFont systemFontOfSize:14.f];
        _usernameField.placeholder = @"请输入用户名";
        _usernameField.keyboardType = UIKeyboardTypeDefault;
        _usernameField.returnKeyType=UIReturnKeyNext;
        _usernameField.textLimitInputType =LTTextLimitInputTypeNone;
        _usernameField.borderStyle = UITextBorderStyleNone;
        _usernameField.leftViewMode = UITextFieldViewModeAlways;
        _usernameField.textColor = [UIColor whiteColor];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 34, 22)];
        imageView.image = [UIImage imageNamed:@"username_login"];
        imageView.contentMode = UIViewContentModeCenter;
        _usernameField.leftView = imageView;
        _usernameField.text=[[NSUserDefaults standardUserDefaults] objectForKey:@"usernamefiled"];;
        _usernameField.delegate=self;
        _usernameline= [[UIImageView alloc] initWithFrame:CGRectMake(0, _usernameField.frame.size.height-1, _usernameField.frame.size.width, 1)];
        _usernameline.backgroundColor=[UIColor whiteColor];
        [_usernameField addSubview:_usernameline];
    }
    return _usernameField;
}
-(LTLimitTextField *)passwordField{
    if (!_passwordField) {
        _passwordField = [[LTLimitTextField alloc]initWithFrame:CGRectZero];
        _passwordField.backgroundColor = [UIColor clearColor];
        _passwordField.font = [UIFont systemFontOfSize:14.f];
        _passwordField.placeholder = @"请输入密码";
        _passwordField.keyboardType = UIKeyboardTypeDefault;
        _passwordField.returnKeyType=UIReturnKeyDone;
        _passwordField.textLimitInputType =LTTextLimitInputTypeNone;
        _passwordField.borderStyle = UITextBorderStyleNone;
        _passwordField.leftViewMode = UITextFieldViewModeAlways;
        _passwordField.secureTextEntry = YES;
        _passwordField.textColor = [UIColor whiteColor];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 34, 22)];
        imageView.image = [UIImage imageNamed:@"password_login"];
        imageView.contentMode = UIViewContentModeCenter;
        _passwordField.leftView = imageView;
        _passwordField.text=[[NSUserDefaults standardUserDefaults] objectForKey:@"passwordfiled"];
        _passwordline= [[UIImageView alloc] initWithFrame:CGRectMake(0, _passwordField.frame.size.height-1, _passwordField.frame.size.width, 1)];
        _passwordline.backgroundColor=[UIColor whiteColor];
        [_passwordField addSubview:_passwordline];
        _passwordField.delegate=self;
    }
    return _passwordField;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    // 如果光标在用户名，切换到密码
    if (textField == self.usernameField) {
        // 密码成为第一响应者
        [self.passwordField becomeFirstResponder];
    } else if (textField == self.passwordField) {
        // 输入焦点就在密码框中
        // 如果是密码，直接调用登录方法
        [self loginClick];
        
        [self.passwordField resignFirstResponder];
    }
    return YES;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_usernameField resignFirstResponder];
     [_passwordField resignFirstResponder];
}
- (UIButton *)sendButton{
    
    if (!_sendButton) {
        _sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _sendButton.backgroundColor=[UIColor whiteColor];
        _sendButton.layer.cornerRadius=20;
        _sendButton.layer.masksToBounds=YES;
        [_sendButton setTitle:@"登录" forState:UIControlStateNormal];
        [_sendButton setTitleColor:UIColorOfHex(0x1E90FF) forState:UIControlStateNormal];
        [_sendButton addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _sendButton;
    
}
-(void)loginClick
{
    if (self.usernameField.text.length==0) {
        [BaseToast show:@"请输入用户账号！"];
        return;
    }
    if (self.passwordField.text.length==0) {
        [BaseToast show:@"请输入密码！"];
        return;
    }
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setValue:@"1" forKey:@"isRecord"];
    [user setValue:self.usernameField.text forKey:@"usernamefiled"];
    [user setValue:self.passwordField.text forKey:@"passwordfiled"];
    [user synchronize];
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    [params setValue:_usernameField.text forKey:@"account"];
    [params setValue:[BaseShare md5:_passwordField.text] forKey:@"password"];
    [[Api sharedApi] login:params callback:^(ApiResponse *response,NSString* errorMsg) {
       if(response.code==0 && response.data !=nil)
       {
           UserModel *model = [UserModel modelWithDictionary:response.data];
           [[BaseShare sharedBaseShare] cacheToken:model.token];
           [[BaseShare sharedBaseShare] cacheUser:response.data];
           [(AppDelegate*)[UIApplication sharedApplication].delegate switchRootViewController];
         
       }else{
           [BaseToast show:response.message];
       }
    }];
    
}
-(void)forgetClick
{
    ForgetPwdViewController *forget =[[ForgetPwdViewController alloc] init];
    BaseNavigationController *loginnav = [[BaseNavigationController alloc] initWithRootViewController:forget];
    [self presentViewController:loginnav animated:YES completion:nil];
}
@end
